﻿/* Copyright (c) 2009, John Cronin
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysila
{
    partial class x86_64_Assembler
    {
        IEnumerable<OutputBlock> x86_64_throw(ThreeAddressCode.Op op, var result, var op1, var op2,
            ThreeAddressCode tac, AssemblerState state)
        {
            List<OutputBlock> ret = new List<OutputBlock>();

            List<OutputBlock> call_block = new List<OutputBlock>();
            byte jmp_dist = 0;
            string target = "throw";

            if (op1.type == var.var_type.Const)
                target = "sthrow";

            x86_64_push(op1, call_block, state);
            call_block.Add(new CodeBlock { Code = new byte[] { 0xe8 } });
            call_block.Add(new RelocationBlock
                {
                    RelType = ElfLib.Elf64_Rela_Shdr.Elf64_Rela.RelocationType.R_X86_64_PLT32,
                    Size = 4,
                    Target = target,
                    Value = -4
                });
            call_block.Add(new CodeBlock { Code = EncOpcode(0, Rsp, 3, true, 0, 0x83) });
            call_block.Add(new CodeBlock { Code = new byte[] { 0x8 } });

            jmp_dist = (byte)GetLongestBlockLength(call_block);

            switch (op)
            {
                case ThreeAddressCode.Op.throweq:
                    ret.Add(new CodeBlock(new byte[] { 0x75, jmp_dist }, new x86_64_Instruction { opcode = "jne", Operand1 = new const_location { c = jmp_dist } }));
                    break;
                case ThreeAddressCode.Op.throwne:
                    ret.Add(new CodeBlock(new byte[] { 0x74, jmp_dist }, new x86_64_Instruction { opcode = "je", Operand1 = new const_location { c = jmp_dist } }));
                    break;
                case ThreeAddressCode.Op.throw_ovf:
                    ret.Add(new CodeBlock(new byte[] { 0x71, jmp_dist }, new x86_64_Instruction { opcode = "jno", Operand1 = new const_location { c = jmp_dist } }));
                    break;
                case ThreeAddressCode.Op.throw_ovf_un:
                    ret.Add(new CodeBlock(new byte[] { 0x73, jmp_dist }, new x86_64_Instruction { opcode = "jnc", Operand1 = new const_location { c = jmp_dist } }));
                    break;
            }
            ret.AddRange(call_block);
            return ret;
        }
    }
}
